/**
 * @Author ZhangCuirong
 * @Date 2025/8/29 8:32
 * @description:
 */
public class Solution1094 {

    public boolean carPooling(int[][] trips, int capacity) {
        int[] diff = new int[1001];
        int[] res = new int[1001];
        for (int i = 0; i < trips.length; i++) {
            int numPassengers = trips[i][0];
            int from = trips[i][1];
            int to = trips[i][2] - 1;
            diff[from] += numPassengers;
            if (to + 1 < diff.length) {
                diff[to + 1] -= numPassengers;
            }
        }
        res[0] = diff[0];
        if (res[0] > capacity) {
            return false;
        }
        for (int i = 1; i < res.length; i++) {
            res[i] = res[i - 1] + diff[i];
            if (res[i] > capacity) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution1094 solution = new Solution1094();
        System.out.println(solution.carPooling(new int[][]{{2, 1, 5}, {3, 3, 7}}, 4));
        System.out.println(solution.carPooling(new int[][]{{2, 1, 5}, {3, 3, 7}}, 5));
        System.out.println(solution.carPooling(new int[][]{{2, 1, 5}, {3, 5, 7}}, 3));
    }
}
